Hint: Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
# load required libraries
# to use harry potter dataset
# devtools::install_github("bradleyboehmke/harrypotter")
# devtools::install_github("quanteda/quanteda.sentiment")
# devtools::install_github("quanteda/quanteda.corpora")
library(quanteda)
library(readtext)
Registered S3 method overwritten by 'data.table':
method from
print.data.table
library(corpus)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ──────────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.5 ✓ purrr 0.3.4
✓ tibble 3.1.4 ✓ dplyr 1.0.7
✓ tidyr 1.1.3 ✓ stringr 1.4.0
✓ readr 2.0.1 ✓ forcats 0.5.1
── Conflicts ─────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(stringr)
library(tidytext)
library(harrypotter)
library(janeaustenr)
library(dplyr)
library(quanteda.sentiment)
library(vader)
# load afinn lexicon
# manually -> convert to binary lexicon
afinn_dict <- read.csv("lexika/AFINN-111.txt", header = F, sep = "\t", stringsAsFactors = F)
afinn_binary <- dictionary(list(positive = afinn_dict$V1[afinn_dict$V2>0], negative = afinn_dict$V1[afinn_dict$V2<0]))
# provided via tidytext?
afinn <- get_sentiments("afinn")
dfm.sentiment <- dfm(korpus, dictionary = afinn_binary)
Warnung: 'dfm.corpus()' is deprecated. Use 'tokens()' first.
Warnung: 'dictionary' and 'thesaurus' are deprecated; use dfm_lookup() instead
dfm.sentiment
Document-feature matrix of: 12 documents, 2 features (0.00% sparse) and 1 docvar.
features
docs positive negative
dal in 241 173
d-heade 211 153
of 180 155
scombe Valley 217 262
ve Ora 151 198
n with the Twi 195 225
[ reached max_ndoc ... 6 more documents ]